package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LocationEditActivity;
import com.github.postsanf.yinian.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MapLocationListAdapter extends RecyclerView.Adapter<MapLocationListAdapter.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private Context context;
    private List<PoiItem> datas = new ArrayList<>();
    private View mHeaderView;
    public static Resources mResource;
    private Activity activity;
    private String ll;
    private boolean mainLocation;

    public MapLocationListAdapter(Context context, List<PoiItem> datas, String ll, boolean mainLocation) {
        this.context = context;
        this.datas = datas;
        this.activity = (Activity) context;
        this.ll = ll;
        this.mainLocation = mainLocation;
    }

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout location;                            // 列表View
        TextView name;                        // 组性质
        TextView address;


        public ViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            location = (LinearLayout) itemView
                    .findViewById(R.id.normal);
            name = (TextView) itemView
                    .findViewById(R.id.location_item_tt);
            address = (TextView) itemView
                    .findViewById(R.id.location_item_des);
        }

        public void bindData(final PoiItem listItems) {
            if (listItems != null) {
                if (!StringUtils.isEmpty(ll) && listItems.getTitle().equals(ll)) {
                    name.setTextColor(context.getResources().getColor(R.color.yncolor));
                    address.setTextColor(context.getResources().getColor(R.color.yncolor));
                } else {
                    name.setTextColor(Color.parseColor("#333333"));
                    address.setTextColor(Color.parseColor("#333333"));
                }
                name.setText(listItems.getTitle());
                address.setText(listItems.getSnippet());
                if (mainLocation) {
                    location.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            activity.startActivity(new Intent(activity, LocationEditActivity.class).putExtra("locationtx", listItems.getTitle()));
                        }
                    });
                } else {
                    location.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                        name.setTextColor(context.getResources().getColor(R.color.yncolor));
//                        address.setTextColor(context.getResources().getColor(R.color.yncolor));
                            context.sendBroadcast(new Intent("updatelocationtx").putExtra("locationtx", listItems.getTitle()));
                            activity.finish();
                        }
                    });
                }
            }
        }
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    private PoiItem getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_location_item, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        PoiItem listItems = getItem(pos);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }


    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }


}
